import { qiniu as QiniuApi, resource as ResApi } from '../../../../../config/request.js'
import {
    resUploadUtil as ResUtil
} from '../../../../../util/';
module.exports = {
    name: 'audio_res',
    data() {
        return {
            form: {
                token: '9mRGBQF0d_kxbHTJrYajWKXioKCvQyBSFupBatdr:UouzEwuQ1mJ_C-mTEtlmqRVuijw=:eyJzY29wZSI6ImFpYmVpIiwiZGVhZGxpbmUiOjE0OTgxMTY1Njd9',
                key: null
            },
            showUploadList: false,
            fileName: '',
            fileSize: '',
            loaded: '',
            percent: '',
            result: '',
            type: '',

            tableData: [],
            updateFormVisible: false,
            resForm: {
                id: '',
                url: '',
                name: '',
                remark: '',
            },

            currentPage: 1,
            tableTotal: 10,
            loading: false,
            tableCount: ResUtil.tableCount,
            layout: ResUtil.layout,
            fields: ResUtil.resPublicFields,
            size: ResUtil.sizeFields,
            time: ResUtil.timeFields,
            remark: ResUtil.remarkFields,
        }
    },
    methods: {
        /**
         * 饿了么ui上传七牛云 上传前回调
         */
        beforeUpload(file) {
            ResUtil.beforeUpload(this, file);
        },
        /**
         * 饿了么ui上传七牛云 上传中回调
         */
        handleProgress(event, file, fileList) {
            ResUtil.handleProgress(this, event, file, fileList);
        },
        /**
         * 饿了么ui上传七牛云 上传成功回调
         */
        handleSuccess(response, file, fileList) {
            ResUtil.handleSuccess(this, response, file, fileList);
        },
        /**
         * 饿了么ui上传七牛云 上传失败回调
         */
        handleError(error, response, file) {
            ResUtil.handleError(this, error, response, file)
        },
        /**
         * 格式化时间
         */
        formatterTime(item) {
            return ResUtil.formatterTime(item);
        },
        /**
         * 格式化大小
         */
        formatterSize(item) {
            return ResUtil.formatterSize(item);
        },
        /**
         * 页码改变的事件
         * @param {any} currentPage 跳转后页码
         */
        handleCurrentChange(currentPage) {
            ResUtil.handleCurrentChange(this, currentPage);
        },
        /** 
         * 提交表单
         */
        submitForm(formName) {
            ResUtil.submitForm(this, formName)
        },
        /**
         * 校验权限 是否有管理员权限
         */
        checkLevel() {
            return ResUtil.checkLevel(this);
        },
        /**
         * 删除资源
         */
        onDelete(res, index) {
            ResUtil.onDelete(this, res, index);
        },
        /**
         * 修改资源
         */
        onEidt(item) {
            ResUtil.onEidt(this, item);
        },
        /**
         * 获取七牛上传token
         */
        _getQiniuToken() {
            QiniuApi.getQiniuToken.call(this, data => {
                this.$message.success('token刷新成功请重新上传资源');
                this.$store.dispatch('update_qiniu_token', {
                    token: data['token'],
                });
            })
        },
        /**
         * 上传资源成功后 数据写入库
         */
        _write2DB(type, fileName, url, size) {
            if (type) {
                if (ResUtil._isImage(type)) { //音频
                    this._uploadImage(fileName, type, url, size);
                } else if (ResUtil._isVideo(type)) { //视频
                    this._uploadVideo(fileName, type, url, size);
                } else if (ResUtil._isAudio(type)) { //音频
                    this._uploadAudio(fileName, type, url, size);
                } else { //其他
                    this._uploadOther(fileName, type, url, size);
                }
            }
        },
        /**
         * 音频资源写入库
         */
        _uploadImage(name, type, url, size) {
            QiniuApi.uploadImage.call(this, {
                name,
                type,
                url,
                size,
            }, data => { this.$message.success('上传图片资源成功 ！！请在刷新音频资源查看'); })
        },
        /**
         * 视频资源写入库
         */
        _uploadVideo(name, type, url, size) {
            QiniuApi.uploadVideo.call(this, {
                name,
                type,
                url,
                size,
            }, data => { this.$message.success('上传视频资源成功 ！！请在刷新视频资源查看'); })
        },
        /**
         * 音频资源写入库
         */
        _uploadAudio(name, type, url, size) {
            QiniuApi.uploadAudio.call(this, {
                name,
                type,
                url,
                size,
            }, data => { this.$message.success('上传音频资源成功 ！！请在刷新音频资源查看'); })
        },
        /**
         * 其他资源写入库
         */
        _uploadOther(name, type, url, size) {
            QiniuApi.uploadOther.call(this, {
                name,
                type,
                url,
                size,
            }, data => { this.$message.success('上传资源成功 ！！请在刷新其他资源查看'); })
        },
        //上面是上传共有api 
        /* ************************************************************************** */
        //下面是上传音频特有的api
        /**
         * 格式化备注
         */
        formatterRemark(item) {
            return item.remark;
        },
        /**
         * 获取资源
         */
        _getRes() {
            this._getAudios();
        },
        /**
         * 删除资源
         */
        _deleteRes(audio) {
            this._deleteAudio(audio);
        },
        /**
         * 修改资源
         */
        _updateRes(formData) {
            this._updateAudio(formData);
        },
        /**
         * 获取音频资源
         */
        _getAudios() {
            this.loading = true;
            ResApi.getAudios.call(this, { page: this.currentPage }, data => {
                console.log(data)
                this.tableData = data['lit'];
                this.tableTotal = data['count'];
                this.loading = false;
            })
        },
        /**
         * 删除音频资源
         */
        _deleteAudio(audio) {
            ResApi.deleteAudio.call(this, audio.id, (result) => {
                let index = this.tableData.indexOf(audio)
                if (index !== -1) {
                    this.tableData.splice(index, 1);
                }
                this.$message.success('删除音频成功');
            })
        },
        /**
         * 修改音频资源
         */
        _updateAudio(formData) {
            ResApi.updateAudio.call(this, formData, data => {
                this.$message.success('修改音频资源成功 ！！');
                this.updateFormVisible = false;
            })
        },
    },

    created: function() {
        this.currentPage = 1;
        this._getRes();
        this._getQiniuToken();
    }
}